cmake_minimum_required(VERSION 3.8)
project(route_planner)

# Default to C++17
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# Find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(std_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(visualization_msgs REQUIRED)

find_package(common REQUIRED)
find_package(semantic_map_manager REQUIRED)
find_package(Boost REQUIRED COMPONENTS system)
find_package(OpenCV REQUIRED)
find_package(Eigen3 REQUIRED)

# Include directories
include_directories(
    include
    ${common_INCLUDE_DIRS}
    ${semantic_map_manager_INCLUDE_DIRS}
    ${EIGEN3_INCLUDE_DIR}
    ${OpenCV_INCLUDE_DIRS}
)

# Create core library
add_library(route_planner_lib SHARED
  src/route_planner/route_planner.cc
)

# Add ROS2 dependencies to core library
ament_target_dependencies(route_planner_lib
  rclcpp
  std_msgs
  geometry_msgs
  sensor_msgs
  visualization_msgs
)

target_link_libraries(route_planner_lib
    common::hkust_pl_common
    semantic_map_manager::hkust_pl_smm
    ${Boost_LIBRARIES}
    ${OpenCV_LIBS}
)

# Set target properties
target_include_directories(route_planner_lib PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
)

# Install libraries
install(TARGETS route_planner_lib
  EXPORT export_${PROJECT_NAME}
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
)

# Install headers
install(DIRECTORY include/
  DESTINATION include
)

# Install config files
install(DIRECTORY
  config
  DESTINATION share/${PROJECT_NAME}/
)

# Export dependencies
ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)
set(EXPORT_DEPENDENCIES
  rclcpp
  std_msgs
  geometry_msgs
  sensor_msgs
  visualization_msgs
)
if(common_FOUND)
  list(APPEND EXPORT_DEPENDENCIES common)
endif()
if(semantic_map_manager_FOUND)
  list(APPEND EXPORT_DEPENDENCIES semantic_map_manager)
endif()
ament_export_dependencies(${EXPORT_DEPENDENCIES})

# Export include directories
ament_export_include_directories(include)

# Testing
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()